package com.hudongwx.godoffood.activity.Community_Activity;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hudongwx.godoffood.R;
import com.hudongwx.godoffood.activity.LoginActivity;
import com.hudongwx.godoffood.utils.MyApplication;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.x;

/**
 * 发送的界面
 */
@ContentView(R.layout.activity_new_message)
public class NewMessageActivity extends AppCompatActivity {
    private PopupWindow iconPopwindow;
    private RelativeLayout iconRelative;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
    }
    @Event(R.id.msg_back)
    private void back(View view){
        finish();
    }

    @Event(R.id.msg_send)
    private void send(View view){
        if (MyApplication.getStorage("Uid").equals("1")){
            Toast.makeText(NewMessageActivity.this,"网络异常",Toast.LENGTH_SHORT).show();
        }else {
            startActivity(new Intent(NewMessageActivity.this, LoginActivity.class));
        }
    }

    @Event(R.id.msg_image)
    private void openSystem(View view){
        popShow(view);
    }


    /**
     * pop弹窗 实现更换头像
     *
     * @param view
     */
    private void popShow(View view) {
        if (null == iconPopwindow) {
            iconRelative = (RelativeLayout) LayoutInflater.from(NewMessageActivity.this).inflate(R.layout.my_head_popup, null);
            iconPopwindow = new PopupWindow(iconRelative, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
            iconPopwindow.showAtLocation(view, Gravity.TOP, 0, 0);
            final LinearLayout llContent = (LinearLayout) iconRelative.findViewById(R.id.head_pop_content_ll);
            final ImageView ivBkg = (ImageView) iconRelative.findViewById(R.id.head_pop_bkg_iv);
            //点击动画
            Animation animation = new TranslateAnimation(
                    Animation.RELATIVE_TO_SELF, 0.0f,
                    Animation.RELATIVE_TO_SELF, 0.0f,
                    Animation.RELATIVE_TO_SELF, 1.0f,
                    Animation.RELATIVE_TO_SELF, 0.0f);
            animation.setDuration(400);
            animation.setInterpolator(new LinearInterpolator());
            transAnimOpen(ivBkg);
            llContent.setAnimation(animation);
            //点击取消
            TextView tvCancel = (TextView) llContent.findViewById(R.id.head_pop_cancel_tv);
            tvCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    closePopupWindows(ivBkg,llContent);
                }
            });
            //点击黑色的区域
            iconRelative.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    closePopupWindows(ivBkg,llContent);
                }
            });
            //跳转到相机
            TextView tvPhoto = (TextView) llContent.findViewById(R.id.head_pop_photo_tv);
            tvPhoto.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    // 指定开启系统相机的Action
                    intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);

                    //从这
                    intent.addCategory(Intent.CATEGORY_DEFAULT);
                    startActivity(intent);
                }
            });
            TextView tvPicture = (TextView) llContent.findViewById(R.id.head_pop_picture_tv);
            tvPicture.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    // 指定开启系统相机的Action
                    intent.setAction(Intent.ACTION_PICK);
                    //从这
                    intent.setType("image/*");
                    startActivity(intent);
                }
            });
        }
    }
    /**
     * 关闭popupWindow
     * @param iv   背景图片
     * @param llContent 布局
     */
    private void closePopupWindows(final ImageView iv, final LinearLayout llContent) {
        //清除动画
        llContent.clearAnimation();
        Animation animation = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 1.0f);
        animation.setDuration(400);
        animation.setInterpolator(new LinearInterpolator());
        llContent.setAnimation(animation);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                transAnimClose(iv);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                iconPopwindow.dismiss();
                iconPopwindow = null;
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    /**
     * 背景出现动画
     *
     * @param iv
     */
    private void transAnimOpen(final ImageView iv) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(iv, "alpha", 0, 0.2f, 0.5f, 0.7f, 0.9f, 1);
        animator.setDuration(400);
        animator.setRepeatCount(0);
        animator.start();
        iv.setVisibility(View.VISIBLE);
    }

    /**
     * 背景消失动画
     *
     * @param iv
     */
    private void transAnimClose(final ImageView iv) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(iv, "alpha", 1, 0.9f, 0.7f, 0.5f, 0.2f, 0);
        animator.setDuration(400);
        animator.setRepeatCount(0);
        animator.start();
        animator.addListener(new Animator.AnimatorListener() {

            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                iv.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
    }

}
